<?php

class Simpananb_model extends Model {

	var $data;
	
	function Simpananb_model()
	{
		parent::Model();
	}
	
	function set_anggota_id($anggota_id)
	{
		$this->db->where('simpananb.anggota_id', $anggota_id);
	}

	function get_all_data(){
		$this->db->select('simpananb.*, anggota.tempat_lahir, anggota.nama_lengkap as nama_anggota, SUM(transaksi_simp.biaya) as saldo_simp');
		$this->db->from('simpananb');
		$this->db->join('anggota', 'simpananb.anggota_id=anggota.id_anggota', 'INNER');
		//$this->db->join('jenis_simp', 'simpananb.jenis_simp=jenis_simp.id', 'INNER');
		$this->db->join('transaksi_simp', 'simpananb.id=transaksi_simp.no_rekening_id', 'left');
		$this->db->group_by('simpananb.id');
		$query = $this->db->get();
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		else
		{
			return FALSE;
		}
	}
	
	
	function get_last_id(){
		$query = $this->db->query('select *, CONCAT(SUBSTR(no_rekening, 8, 2),SUBSTR(no_rekening, 5, 2),SUBSTR(no_rekening, 11, 4)) as urut from simpananb order by urut desc limit 1 ');
		if ($query->num_rows() > 0)
		{
			return $query->row();
		}
		else
		{
			return FALSE;
		}
	}

	function get_data_by_id($id){
		//$this->db->select('simpananb.id, simpananb.no_rekening, simpananb.anggota_id, anggota.nama_lengkap as nama_anggota, simpananb.tgl_reg, simpananb.biaya, simpananb.tgl_mulai, simpananb.tgl_selesai, simpananb.keterangan');
		$this->db->select('simpananb.*, anggota.nama_lengkap as nama_anggota, anggota.tgl_lahir as tgl_lahir_anggota, anggota.telpon_1 as telp_anggota, anggota.tempat_lahir as tempat_lahir_anggota, anggota.alamat as alamat_anggota, anggota.pekerjaan as pekerjaan_anggota');
		$this->db->from('simpananb');
		$this->db->join('anggota', 'simpananb.anggota_id=anggota.id_anggota', 'INNER');
		$this->db->where('simpananb.id', $id);
		$query = $this->db->get();
		if ($query->num_rows() > 0)
		{
			return $query->row_array();
		}
		else
		{
			return FALSE;
		}
	}


	function fill_data()
	{
//		echo 'hoho';
//		echo $this->input->post('anggotaID');
		//$status = ($this->input->post('status')) ? $this->input->post('status') : 'Start';
		
		$biaya = $this->input->post('biaya');
		$biaya = str_replace(",00", "", $biaya);
		$biaya = str_replace("Rp", "", $biaya);
		$biaya = str_replace(" ", "", $biaya);
		$biaya = str_replace(".", "", $biaya);
		
		$total = $this->input->post('total');
		$total = str_replace(",00", "", $total);
		$total = str_replace("Rp", "", $total);
		$total = str_replace(" ", "", $total);
		$total = str_replace(".", "", $total);
		
		$bunga_per_bulan = $this->input->post('bunga_per_bulan');
		$bunga_per_bulan = str_replace(",00", "", $bunga_per_bulan);
		$bunga_per_bulan = str_replace("Rp", "", $bunga_per_bulan);
		$bunga_per_bulan = str_replace(" ", "", $bunga_per_bulan);
		$bunga_per_bulan = str_replace(".", "", $bunga_per_bulan);
		
		$total_bunga = $this->input->post('total_bunga');
		$total_bunga = str_replace(",00", "", $total_bunga);
		$total_bunga = str_replace("Rp", "", $total_bunga);
		$total_bunga = str_replace(" ", "", $total_bunga);
		$total_bunga = str_replace(".", "", $total_bunga);
				
		$administrasi = $this->input->post('administrasi');
		$administrasi = str_replace(",00", "", $administrasi);
		$administrasi = str_replace("Rp", "", $administrasi);
		$administrasi = str_replace(" ", "", $administrasi);
		$administrasi = str_replace(".", "", $administrasi);
		
		$this->data = array(
			'no_rekening' => $this->input->post('no_rekening'),
			//'anggota_id' => $this->input->post('anggotaID'),
			'anggota_id' => $this->input->post('anggota'),
			'tgl_reg' => $this->input->post('tgl_reg'),
			'tgl_selesai' => $this->input->post('tgl_selesai'),
			'masa_simpanan' => $this->input->post('masa_simpanan'),
			'bunga_persen' => $this->input->post('bunga_persen'),
			'administrasi' => $administrasi,
			'biaya' => $biaya,
			'total' => $total,
			'bunga_per_bulan' => $bunga_per_bulan,
			'total_bunga' => $total_bunga,
			'keterangan' => $this->input->post('keterangan')
		);
	}
	
	function fill_data_ambil()
	{		
		$this->data = array(
			'status_ambil' => 1
		);
	}
	
	//Check for duplicate login ID
	function check_name($id = '')
	{
		$this->db->where('no_rekening', $this->data['no_rekening']);
		if($id != '') $this->db->where('id !=', $id);
		$query = $this->db->get('simpananb');

		if ($query->num_rows() > 0)
		{
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}	

	function insert_data()
	{
		$insert = $this->db->insert('simpananb', $this->data);
		return $insert;
	}

	function update_data($id)
	{
		$this->db->where('id', $id);
		$update = $this->db->update('simpananb', $this->data);
		return $update;
	}
	
	function update_data_ambil($id)
	{
		$this->db->where('id', $id);
		$update = $this->db->update('simpananb', $this->data);
		return $update;
	}

	function delete_data($id){
		$this->db->where('id', $id);
		$delete = $this->db->delete('simpananb');
		return $delete;
	}
	

}

/* End of file simpananb_model.php */
/* Location: ./application/models/simpananb_model.php */



